package com.xoa.dev.pz.service;

import com.xoa.model.users.Users;
import com.xoa.dev.pz.dao.PzDailyPeaceMapper;
import com.xoa.dev.pz.dao.PzPersonMapper;
import com.xoa.dev.pz.model.PzDailyPeace;
import com.xoa.service.users.UsersService;
import com.xoa.util.DateFormat;
import com.xoa.util.ToJson;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.page.PageParams;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * description 信访稳定-每日平安稳定信息接口
 * Created by 刘新婷 on 2017/12/7.
 */
@Service
public class PzDailyPeaceService {

    @Resource
    private PzDailyPeaceMapper pzDailyPeaceMapper;
    @Resource
    private UsersService usersService;
    @Resource
    private PzPersonMapper pzPersonMapper;
    /**
     * @description: : 添加每日平安稳定信息
     * @createBy:  刘新婷
     * @createTime:  2017-12-07
     * @param request
     * @param pzDailyPeace
     * @return
     */
    public ToJson<PzDailyPeace> insertSelective(HttpServletRequest request, PzDailyPeace pzDailyPeace){
        ToJson<PzDailyPeace> json = new ToJson<>();
        Users user= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
        pzDailyPeace.setCreateUser(user.getUserId());
        pzDailyPeace.setCreateDate(DateFormat.getDatestr(new Date()));
        try{
            pzDailyPeaceMapper.insertSelective(pzDailyPeace);
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setMsg("err");
            json.setFlag(1);
        }
        return json;
    }

    /**
     * @description: : 修改每日平安稳定信息
     * @createBy:  刘新婷
     * @createTime:  2017-12-07
     * @param pzDailyPeace
     * @return
     */
    public ToJson<PzDailyPeace> updateByPrimaryKeySelective (PzDailyPeace pzDailyPeace){
        ToJson<PzDailyPeace> json = new ToJson<>();
        try {
            int count = pzDailyPeaceMapper.updateByPrimaryKeySelective(pzDailyPeace);
            if(count > 0){
                json.setMsg("ok");
                json.setFlag(0);
            }else {
                json.setMsg("err");
                json.setFlag(1);
            }

        }catch (Exception e){
            e.printStackTrace();
            json.setMsg("err");
            json.setFlag(1);
        }
        return json;
    }

    /**
     * @description: : 删除每日平安稳定信息
     * @createBy:  刘新婷
     * @createTime:  2017-12-07
     * @param dailyId
     * @return
     */
    public ToJson<PzDailyPeace> deleteByPrimaryKey(Integer dailyId){
        ToJson<PzDailyPeace> json = new ToJson<>();
        try {
            pzDailyPeaceMapper.deleteByPrimaryKey(dailyId);
            json.setFlag(0);
            json.setMsg("ok");

        }catch (Exception e){
            e.printStackTrace();
            json.setMsg("err");
            json.setFlag(1);
        }
        return json;
    }

    /**
     * @description: : 查询每日平安稳定列表信息
     * @createBy:  刘新婷
     * @createTime:  2017-12-07
     * @param output
     * @param pzDailyPeace
     * @param page
     * @param pageSize
     * @param useFlag
     * @param request
     * @param response
     * @return
     */
    public ToJson<PzDailyPeace> queryList(Integer output, PzDailyPeace pzDailyPeace, Integer page, Integer pageSize, boolean useFlag,
                                          HttpServletRequest request, HttpServletResponse response){
        ToJson<PzDailyPeace> json = new ToJson<PzDailyPeace>();
        if("".equals(useFlag)){useFlag = true;}
        String s  = pzDailyPeace.getCreateDate();
        PageParams pageParams=new PageParams();
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        pageParams.setUseFlag(useFlag);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("page", pageParams);
        map.put("pzDrug",pzDailyPeace);
        try{
            List<PzDailyPeace> pzDailyPeaceList =  pzDailyPeaceMapper.queryList(map);
            for (PzDailyPeace pz :pzDailyPeaceList) {
                String[] arr = pz.getFillinDate().split("-");
                String date = arr[0]+"年"+arr[1]+"月"+arr[2]+"日";
                pz.setFillinDate(date);
                pz.setGridUserNames(usersService.getUserNameById(pz.getCreateUser()));
            }
            json.setObj(pzDailyPeaceList);
            json.setTotleNum(pageParams.getTotal());
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setMsg("err");
            json.setFlag(1);
        }
        return json;


    }


    /**
     * @description: : 根据id查询每日平安稳定信息
     * @createBy:  刘新婷
     * @createTime:  2017-12-07
     * @param dailyId
     * @return
     */
    public ToJson<PzDailyPeace> selectById(Integer dailyId){
        ToJson<PzDailyPeace> json = new ToJson<>();
        try {
            PzDailyPeace pzDailyPeace = pzDailyPeaceMapper.selectById(dailyId);
            pzDailyPeace.setGridUserNames(usersService.getUserNameById(pzDailyPeace.getCreateUser()));
            json.setObject(pzDailyPeace);
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setMsg("err");
            json.setFlag(1);
        }
        return json;

    }

    /**
     * @description: : 根据时间查询每日平安稳定信息
     * @createBy:  刘新婷
     * @createTime:  2017-12-07
     * @param startDate
     * @param endDate
     * @return
     */
    public ToJson<PzDailyPeace> selectByCreateDate(String startDate, String endDate){
        ToJson<PzDailyPeace> json = new ToJson<>();
        try {
            List<PzDailyPeace> pzDailyPeaceList =  pzDailyPeaceMapper.selectByCreateDate(startDate,endDate);
            for (PzDailyPeace pz :pzDailyPeaceList) {
                String[] arr = pz.getFillinDate().split("-");
                String date1 = arr[0]+"年"+arr[1]+"月"+arr[2]+"日";
                pz.setFillinDate(date1);
                pz.setGridUserNames(usersService.getUserNameById(pz.getCreateUser()));
            }
            json.setObject(pzDailyPeaceList);
            json.setTotleNum(pzDailyPeaceMapper.countByCreateDate(startDate,endDate));
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setMsg("err");
            json.setFlag(1);
        }
        return json;

    }

}
